﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using System.Xml.Linq;

namespace WikiFromCmd
{
    class Program
    {
        const string urlPattern = @"http://ru.wikipedia.org/w/api.php?action=opensearch&search={0}&prop=info&format=xml&inprop=url";
        static void Main(string[] args)
        {
            if (args.Length == 0)
                return;

            var url = string.Format(urlPattern, HttpUtility.UrlEncode(args[0]));
            var request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.UserAgent += @"User-Agent: MyCuteBot/0.1 r\n\ Connection: Close";
            var response = (HttpWebResponse)request.GetResponse();
            var result = GetXmlResponse(response);
        }

        static XElement GetXmlResponse(HttpWebResponse response)
        {
            var xml = response.GetResponseStream();
            StreamReader streamRead = new StreamReader(xml);
            Char[] readBuffer = new Char[256];
            var str = streamRead.ReadToEnd();
            StringBuilder output = new StringBuilder();
            using (XmlReader reader = XmlReader.Create(new StringReader(str)))
            {
                reader.ReadToFollowing("book");
                reader.MoveToFirstAttribute();
                string genre = reader.Value;
                output.AppendLine("The genre value: " + genre);

                reader.ReadToFollowing("title");
                output.AppendLine("Content of the title element: " + reader.ReadElementContentAsString());
            }
            XmlDocument document = new XmlDocument();
            //document.LoadXml(xml)
            return XElement.Load(xml);
        }

    }
}
